package servlet.market;

import com.fasterxml.jackson.databind.ObjectMapper;
import servlet.util.PageVO;
import service.CskaoyanSqlSessionFactory;
import servlet.util.SqlValues;
import lombok.SneakyThrows;
import mapper.AddressMapper;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/address")
public class Address extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取对应参数和对应值，传参，进行值的处理
        //把内容封装成对象把
        SqlValues sqlValues= new SqlValues();
        BeanUtils.copyProperties(sqlValues,req.getParameterMap());

        sqlValues.setOffset((sqlValues.getPage()-1)* sqlValues.getLimit());

        //返回表的内容
        SqlSession sqlSession= CskaoyanSqlSessionFactory.sqlSessionFactory.openSession();
        AddressMapper addressMapper=sqlSession.getMapper(AddressMapper.class);
        List<bean.model.Address> addressList=addressMapper.getAddress(sqlValues);
        PageVO backValues=new PageVO<>(0,addressList.toArray(),"成功");
        resp.getWriter().println(new ObjectMapper().writeValueAsString(backValues));
    }
}
